package com.daqsoft.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 周麟东 on 10:30
 */
public enum DateUtil {
    INS;

    public String parse(Date date) {
        return parse(date, "yyyy-MM-dd");
    }

    public String parse(Date date, String format) {
        if (date == null){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 计算两个日期差多少天
     * @param one 日期1
     * @param two 日期2
     * @return 长度
     */
    public long distanceDays(Date one, Date two){
        long days;
        long time1 = one.getTime();
        long time2 = two.getTime();
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        days = diff / (1000 * 60 * 60 * 24);
        return days + 1;
    }

    public Date addDay(Date date, int i) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE , i);
        return ca.getTime();
    }
}
